package diplomovka.graphCreator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.bcel.classfile.JavaClass;
import org.jdom.Element;

import diplomovka.graphCreator.modelParts.Type;

public class GraphCreator {
	private List<ModelPartCreator> modelPartCreators;

	public GraphCreator() {
		super();
		modelPartCreators = new ArrayList<ModelPartCreator>();
		modelPartCreators.add(new ClazzCreator());
		modelPartCreators.add(new InterfaceCreator());
		modelPartCreators.add(new EnumerationCreator());
		modelPartCreators.add(new AnnotationCreator());
		modelPartCreators.add(new ExceptionCreator());
	}

	/**
	 * Creates graph, which represents given classes and config files
	 * 
	 */
	public List<Object> createGraphFromLoadedFiles(List<Class> loadedClasses, List<JavaClass> loadedByteCodes,
			Map<String, Element> loadedConfigFiles) {
		List<Object> resultGraph = new ArrayList<Object>();
		int index = 0;
		
		// runs through all loadedClasses and for each create Clazz instance
		for (Class loadedClass : loadedClasses) {
			JavaClass loadedByteCode = loadedByteCodes.get(index++);
			// runs through all modelPart creators and add created model part to
			// the result graph
			for (ModelPartCreator modelPartCreator : modelPartCreators) {
				if (modelPartCreator.canHandle(loadedClass)) {
					Type createdType = modelPartCreator.createModelPart(
							loadedClass, loadedByteCode, loadedConfigFiles);
					resultGraph.add(createdType);
				}
			}
		}

		return resultGraph;
	}
}
